<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Settle;
use App\Http\Model\FinanceKk;
use App\Http\Services\ActivityServices;
use Carbon\Carbon;

class CommandCreateBeforRoomRank extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CommandCreateBeforRoomRank';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成昨日/上周数据';

    protected $activityService;
    protected $log = 'CommandCreateBeforRoomRank';
    private $redis_charts_room_user_wealth_yestorday = "FINANCE:CHARTS:ROOM:USER:WEALTH:YESTORDAY:";//FINANCE:CHARTS:ROOM:USER:WEALTH:YESTORDAY:{room_id}-2018-11-22
    private $redis_charts_room_user_wealth_lastweek = "FINANCE:CHARTS:ROOM:USER:WEALTH:LASTWEEK:";//FINANCE:CHARTS:ROOM:USER:WEALTH:LASTWEEK:{roomid}-2018-11-22|2018-11-22
    private $redis_charts_room_user_charts_yestorday = "FINANCE:CHARTS:ROOM:USER:CHARM:YESTORDAY:"; // FINANCE:CHARTS:ROOM:USER:CHARM:DAY:{room_id}:{ymd}
    private $redis_charts_room_user_charts_lastweek = "FINANCE:CHARTS:ROOM:USER:CHARM:LASTWEEK:";
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->activityService = new ActivityServices();
    }

    protected $financeWealthStreamTable = 'finance_room_wealth_stream';
    protected $financeCharmStreamTable = 'finance_charm_stream';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //昨日
        $yestorday = Carbon::yesterday()->toDateString();
        //上周
        // 获取上周的开始日期（周一）
        $startOfLastWeek = Carbon::now()->subWeek()->startOfWeek()->toDateString();
        
        // 获取上周的结束日期（周日）
        $endOfLastWeek = Carbon::now()->subWeek()->endOfWeek()->toDateString();

        //查看所有CBD房间
        $roomIds = DB::table('union_room')->get();
        $roomIds = objToArray($roomIds);

        if(empty($roomIds)){
            write_log($this->log,'INFO 无数据',$roomIds);
        }
        foreach($roomIds as $room) {
            $this->_roomMakeRank($room['room_id'], $yestorday, $startOfLastWeek, $endOfLastWeek);
            $this->_roomMakeCharmRank($room['room_id'], $yestorday, $startOfLastWeek, $endOfLastWeek);
        }
    }

    //生成房间上周昨日财富缓存
    public function _roomMakeRank($roomId, $yestorday, $startOfLastWeek, $endOfLastWeek){
        do {
            //昨日榜
            $cacheKey = $this->redis_charts_room_user_wealth_yestorday.$roomId.':'.$yestorday;
            $yestordayStartDay = Carbon::parse($yestorday)->startOfDay();
            $yestordayEndDay = Carbon::parse($yestorday)->endOfDay();
            Redis::DEL($cacheKey);
            $list = DB::table($this->financeWealthStreamTable)
                ->where('room_id', $roomId)
                ->where('create_time', '>=',$yestordayStartDay)
                ->where('create_time', '<=',$yestordayEndDay)
                ->groupBy('user_id')
                ->selectRaw('user_id,sum(wealth_value) as num')
                ->get();
            $list = objToArray($list);
            foreach($list as $val) {
                Redis::ZADD($cacheKey, $val['num'], $val['user_id']);
                Redis::EXPIRE($cacheKey, 2 * 24 * 3600);
            }

            //上周榜
            $cacheKey = $this->redis_charts_room_user_wealth_lastweek.$roomId.':'.$startOfLastWeek.'|'.$endOfLastWeek;
            Redis::DEL($cacheKey);
            $list = DB::table($this->financeWealthStreamTable)
                ->where('room_id', $roomId)
                ->where('create_time', '>=', $startOfLastWeek.' 00:00:00')
                ->where('create_time', '<=', $endOfLastWeek.' 23:59:59')
                ->groupBy('user_id')
                ->selectRaw('user_id,sum(wealth_value) as num')
                ->get();
            $list = objToArray($list);
            foreach($list as $val) {
                Redis::ZADD($cacheKey, $val['num'], $val['user_id']);
                Redis::EXPIRE($cacheKey, 2 * 7 * 24 * 3600);
            }
            write_log($this->log,'INFO 房间'.$roomId.'更新完成',[]);
        } while (false);
    }

    //某个房间重新生成房间人气日榜周榜
    public function _roomMakeCharmRank($roomId,  $yestorday, $startOfLastWeek, $endOfLastWeek)
    {
        do {
            //日榜
            $cacheKey = $this->redis_charts_room_user_charts_yestorday.$roomId.':'.$yestorday;
            $yestordayStartDay = Carbon::parse($yestorday)->startOfDay();
            $yestordayEndDay = Carbon::parse($yestorday)->endOfDay();
            Redis::DEL($cacheKey);
            $list = DB::table($this->financeCharmStreamTable)
                ->where('room_id', $roomId)
                ->where('change_time', '>=', $yestordayStartDay)
                ->where('change_time', '<=', $yestordayEndDay)
                ->groupBy('user_id')
                ->selectRaw('user_id,sum(charm_value) as num')
                ->get();
            $list = objToArray($list);
            foreach($list as $val) {
                Redis::ZADD($cacheKey, $val['num'], $val['user_id']);
                Redis::EXPIRE($cacheKey, 2 * 24 * 3600);
            }

            //周榜
            $cacheKey = $this->redis_charts_room_user_charts_lastweek.$roomId.':'.$startOfLastWeek.'|'.$endOfLastWeek;
            Redis::DEL($cacheKey);
            $list = DB::table($this->financeCharmStreamTable)
                ->where('room_id', $roomId)
                ->where('change_time', '>=', $startOfLastWeek.' 00:00:00')
                ->where('change_time', '<=', $endOfLastWeek.' 23:59:59')
                ->groupBy('user_id')
                ->selectRaw('user_id,sum(charm_value) as num')
                ->get();
            $list = objToArray($list);
            foreach($list as $val) {
                Redis::ZADD($cacheKey, $val['num'], $val['user_id']);
                Redis::EXPIRE($cacheKey, 2 * 7 * 24 * 3600);
            }
            write_log($this->log,'INFO 房间'.$roomId.'更新完成',[]);
        } while (false);

    }
}